package com.xyzh.application.config.security;


import com.xyzh.application.base.service.UserServiceImpl;
import com.xyzh.application.common.constants.PermitAllUrl;

import com.xyzh.application.config.wechat.OpenIdAuthenticationProvider;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import org.springframework.security.crypto.password.PasswordEncoder;


/**
 * @author jzb
 * @date 2020/6/28 14:40
 */
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true,prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserServiceImpl userDetailService;

    //认证管理器
    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
    //密码编码器
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new myPasswordEncoder();
    }

//    @Override
//    @Bean
//    public UserDetailsService userDetailsService() {
//        //获取用户账号密码及权限信息
//        return new UserServiceImpl() ;
//    }

    /**
     * 自定义openId验证
     * @return
     */
    @Bean
    public OpenIdAuthenticationProvider openIdAuthenticationProvider(){
        return new OpenIdAuthenticationProvider(userDetailService,passwordEncoder());
    }

    /**
     * 配置默认的密码授权模式的ProviderManager，否则 密码授权 client认证将不会通过。
     * @return
     */
    @Bean
    public AuthenticationProvider daoAuthenticationProvider() {
        DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
        daoAuthenticationProvider.setUserDetailsService(userDetailService);
        daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
        daoAuthenticationProvider.setHideUserNotFoundExceptions(false);
        return daoAuthenticationProvider;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //配置认证方式等
//        super.configure(auth);
        auth.authenticationProvider(daoAuthenticationProvider())
                .authenticationProvider(openIdAuthenticationProvider());
    }




    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //http相关的配置，包括登入登出、异常处理、会话管理等
        http.authorizeRequests()
                .antMatchers(PermitAllUrl.permitAllUrl("/swagger-ui.html","/oauth/**","/author/**","/model/**","/files/**")).permitAll() // 放开权限的url
                .anyRequest().authenticated().and()
                .httpBasic().and().csrf().disable()
                .headers().frameOptions().disable().and()
                .headers().xssProtection().disable();
    }
}
